#include <bits/stdc++.h>
using namespace std;
#define int long long
int N;
int qpow(int x,int n)
{
    int ret=1;
    while(n)
    {
        if(n&1)ret=ret*x%N;
        x=x*x%N;
        n>>=1;
    }
    return ret; 
}
void solve()
{
    int n,b;
    cin>>n>>b>>N;
    int x,y,ans=1;
    for(int i=1;i<=n;i++)
    {
        cin>>x>>y;
        ans=(ans*qpow(x,y))%N;
    }
    ans=(ans*qpow(b, N-2))%N;
    cout<<ans<<endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T=1;
    cin>>T;
    while(T--)
    {
       solve();
    }
    return 0;
}